﻿using CPPEI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SLG.Domain.Services
{
    public class CreateService
    {
        public SingleResult<Datas.KingdomAndKing> CreateKingdom(int userId, int bdRaceId, int bdKingId, string kingName)
        {
            using (SLGDbContext dbContext = new SLGDbContext())
            {
                int serId = Config.ServerId;

                EntRep<Views.V_BD_King> bdkRep = new EntRep<Views.V_BD_King>(dbContext);
                Views.V_BD_King bdKing = bdkRep.Get(p => p.Id == bdKingId);
                if(bdKing == null)
                {
                    return new SingleResult<Datas.KingdomAndKing>(Library.ErrorCodes.Com_ObjectNotExist);
                }

                EntRep<Entities.T_King> kRep = new EntRep<Entities.T_King>(dbContext);
                if (kRep.Count(p => p.ServerId == serId && p.UserId == userId) > 0)
                {
                    return new SingleResult<Datas.KingdomAndKing>(Library.ErrorCodes.Com_ObjectExist);
                }

                if(kRep.Count(p=>p.ServerId == serId && p.Name == kingName) > 0)
                {
                    return new SingleResult<Datas.KingdomAndKing>(Library.ErrorCodes.Com_ObjectExist);
                }

                EntRep<Entities.T_Kingdom> kdRep = new EntRep<Entities.T_Kingdom>(dbContext);
                EntRep<Entities.T_City> cRep = new EntRep<Entities.T_City>(dbContext);
                EntRep<Entities.T_Hero> hRep = new EntRep<Entities.T_Hero>(dbContext);

                DateTime now = DateTime.Now;
                Guid kingdomId = Guid.NewGuid();
                Guid cityId = Guid.NewGuid();
                Guid kingId = Guid.NewGuid();
                Guid heroId = Guid.NewGuid();

                #region 创建王国
                Entities.T_Kingdom kingdom = new Entities.T_Kingdom();
                kingdom.Id = kingdomId;
                kingdom.Name = string.Format("{0}'s Kingdom", kingName);
                kingdom.CreatedTime = now;
                kingdom.ServerId = Config.ServerId;
                kingdom.UserId = userId;
                kingdom.CapitalId = cityId;
                kingdom.KingId = kingId;
                kingdom.BDRaceId = bdRaceId;
                kingdom.Area = 1;
                kingdom.PosX = 1;
                kingdom.PosY = 1;
                kingdom.Level = 1;
                kingdom.Experience = 0;
                kingdom.CreatedTime = now;
                kdRep.Create(kingdom);
                #endregion

                #region 创建capital
                Entities.T_City city = new Entities.T_City();
                city.ServerId = serId;
                city.UserId = userId;
                city.Id = cityId;
                city.KingdomId = kingdomId;
                city.Name = string.Format("{0}'s Capital", kingName);
                city.LordId = heroId;
                city.IsCapital = true;
                city.Area = 1;
                city.PosX = 1;
                city.PosY = 1;
                city.Level = 1;
                city.Experience = 0;
                city.CreatedTime = now;
                city.ResLiquidatedTime = now;
                cRep.Create(city);
                #endregion

                #region 创建king
                Entities.T_King king = new Entities.T_King();
                king.UserId = userId;
                king.ServerId = serId;
                king.Id = kingId;
                king.Name = kingName;
                king.Avatar = bdKing.Avatar;
                king.Level = 1;
                king.Experience = 0;
                king.BDKingId = bdKingId;
                king.BDRaceId = bdRaceId;
                king.Gender = DEF.Genders.Male;
                king.CreatedTime = now;
                kRep.Create(king);
                #endregion

                #region 创建Hero
                Entities.T_Hero hero = new Entities.T_Hero();
                hero.Id = heroId;
                hero.ServerId = serId;
                hero.UserId = userId;
                hero.KingdomId = kingdomId;
                hero.CityId = cityId;
                hero.Name = "Newbie";
                hero.Gender = DEF.Genders.Male;
                hero.Level = 1;
                hero.Experience = 0;
                hero.Avatar = "res/bd/avatar/010.png";
                hero.Status = 0;
                hero.CreatedTime = now;
                hero.Rank = DEF.Ranks.Elite;
                hRep.Create(hero);
                #endregion

                SLG.Domain.Strategy.DefaultBuildingStrategy.Build(dbContext, Config.ServerId,
                    userId,
                    kingdomId,
                    cityId,
                    ref now);

                SLG.Domain.Strategy.DefaultResourceStrategy.Build(dbContext,
                    serId,
                    userId,
                    kingdomId,
                    cityId,
                    ref now);

                try
                {
                    dbContext.SaveChanges();;
                    return new SingleResult<Datas.KingdomAndKing>(new Datas.KingdomAndKing(kingdom, king));
                }
                catch(Exception ex)
                {
                    return new SingleResult<Datas.KingdomAndKing>(Library.ErrorCodes.Com_DbError);
                }
            }
        }
    }
}
